package com.bolt.support.spring.jpa.entity;

import com.bolt.common.utils.StrUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

/**
 * 框架提供一个基础的UUID方式的实体对象定义参考
 * 具体可根据项目考虑选用其他主键如自增、序列等方式，只需修改相关泛型参数类型和主键定义注解即可
 */
@Getter
@Setter
@Access(AccessType.FIELD)
@MappedSuperclass
public class BaseUuidEntity extends BaseEntity<String> {
    @Id
    @Column(length = 40)
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = "org.hibernate.id.UUIDGenerator")
    private String id;

    public void setId(final String id) {
        //容错处理id以空字符提交参数时修改为null避免hibernate主键无效修改
        if (StrUtil.isBlank(id)) {
            this.id = null;
        } else {
            this.id = id;
        }
    }
}
